package com.ellricky.market.ui.act

import android.view.View
import com.andr.common.http.callback.JOCallback
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.entity.ActDetailEntity
import com.ellricky.baselib.http.Api.API_ACT_TASK_DETAIL
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.http.callback.ECallback
import com.ellricky.baselib.ui.base.BaseTakePhotoActivity
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.market.*
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.market_activity_act_detail.*
import kotlinx.android.synthetic.main.market_activity_act_detail_content.*
import org.devio.takephoto.model.TResult
import org.json.JSONObject
import java.io.File


class ActDetailActivity : BaseTakePhotoActivity() {


    private var picPath: String? = null
    private lateinit var id: String
    override fun getContentLayout(): Int {
        return R.layout.market_activity_act_detail
    }

    override fun initUIView() {
        super.initUIView()


        val pageType = intent.getIntExtra(MARKET_ACT_PAGE_TYPE, -1)
        val taskType = intent.getStringExtra(MARKET_ACT_TASK_TYPE)
        id = intent.getStringExtra(MARKET_ACT_ID) ?: ""
        mTvTitle.text = when (pageType) {
            MARKET_TYPE_GROUND_SPREAD -> getString(R.string.market_ground_spread_detail)
            MARKET_TYPE_ACT -> getString(R.string.market_act_detail)
            MARKET_TYPE_TASK -> getString(R.string.market_task_detail)
            else -> ""
        }

        detailView.setContentView(R.layout.market_activity_act_detail_content)


        rlFz.visibility = when (pageType) {
            MARKET_TYPE_GROUND_SPREAD -> View.GONE
            MARKET_TYPE_TASK -> {
                when (taskType) {
                    "7" -> View.GONE
                    else -> View.VISIBLE
                }
            }
            else -> View.VISIBLE
        }

        picView.setListener(this)
        btnSubmit.setOnClickListener(this)

        loadDetailData(id)

    }


    private fun loadDetailData(id: String) {
        val url = EAppUtil.getApiHost() + API_ACT_TASK_DETAIL
        val paramMap = mutableMapOf<String, Any>("id" to id)
        HttpUtils.getInstance().post(url, paramMap, object : ECallback<ActDetailEntity>(this) {
            override fun onSuccess(data: ActDetailEntity?, msg: String) {

                tvActTitle.text = data?.title
                tvFz.text = data?.name
                tvStartTime.text = data?.taskStartDate
                tvEndTime.text = data?.taskEndDate
                tvJl.text = data?.giveScore
                tvCf.text = data?.removeScore
                tvCy.text = data?.attendName
                tvDesc.text = data?.taskText

                picView.visibility = View.VISIBLE
                val attended = !EmptyUtil.isEmpty(data?.picUrl)
                picView.setPic(data?.picUrl)
                btnSubmit.visibility =
                        if (attended) View.GONE
                        else View.VISIBLE
            }
        })
    }


    override fun onClickView(v: View?) {
        super.onClickView(v)
        if (v == btnSubmit) {
            // 接口提交
            if (EmptyUtil.isEmpty(picPath)) {
                ToastUtil.show(this, "图片路径为空")
                return
            }

            picPath?.let {
                val url = EAppUtil.getApiHost() + API_MARKET_PIC_UPLOAD
                val file = File(it)
                val paramMap = hashMapOf("id" to id)
                HttpUtils.getInstance().uploadPhoto(url, file, paramMap, object : JOCallback(this) {
                    override fun onSuccess(dataJO: JSONObject?, msg: String?) {
                        setResult(RESULT_CODE_ACT_DETAIL)
                        finish()
                    }

                })
            }
        }

    }

    override fun takeSuccess(result: TResult) {
        Logger.t("detail").e("%s", result.image.compressPath)
        picPath = result.image.compressPath
        picView.setPic(picPath)
    }


}
